package org.csu.jpetstore.service.impl;

import org.csu.jpetstore.entity.Sequence;
import org.csu.jpetstore.mapper.SequenceMapper;
import org.csu.jpetstore.service.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SequenceServiceImpl implements SequenceService {
    @Autowired
    private SequenceMapper sequenceMapper;

    /**
     * 根据名(Sequence中的主键)获取下一个Id,并对其进行增长
     *
     * @param name 名
     * @return Sequence
     * @author huangzhangyan
     * @date 2022/4/13 20:43
     */
    public Sequence getNextIdByName(String name) {
        Sequence sequence = sequenceMapper.selectById(name);
        if (sequence == null) {
            throw new RuntimeException();
        }
        sequence.setNextId(sequence.getNextId() + 1);
        sequenceMapper.updateById(sequence);
        sequence.setNextId(sequence.getNextId() - 1);
        return sequence;
    }
}
